package com.patent.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

	private static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

	/***
	 * 两个日期相差多少秒
	 */
	public static int getTimeDelta(String dateStr1,String dateStr2){
		Date date1=parseDateByPattern(dateStr1, yyyyMMddHHmmss);
		Date date2=parseDateByPattern(dateStr2, yyyyMMddHHmmss);
		return getTimeDelta(date1, date2);
	}

	public static int getTimeDelta(Date date1,Date date2){
		long timeDelta=(date1.getTime()-date2.getTime())/1000;//单位是秒
		int secondsDelta=timeDelta>0?(int)timeDelta:(int)Math.abs(timeDelta);
		return secondsDelta;
	}

	public static Date parseDateByPattern(String dateStr,String dateFormat){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			return sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * date2比date1多的天数
	 */
	 public static int differentDays(Date date1,Date date2) {
	 	Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1);
	 	Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2);
	 	int day1= cal1.get(Calendar.DAY_OF_YEAR);
	 	int day2 = cal2.get(Calendar.DAY_OF_YEAR);
	 	int year1 = cal1.get(Calendar.YEAR);
	 	int year2 = cal2.get(Calendar.YEAR);
	 	if(year1 != year2) {
	 		int timeDistance = 0 ;
	        for(int i = year1 ; i < year2 ; i ++)  {
	    	    if(i%4==0 && i%100!=0 || i%400==0) {
	    	    	timeDistance += 366;
	    	    }  else {
	    	    	timeDistance += 365;
	    	    }
	        }
	        return timeDistance + (day2-day1) ;
	 	} else {
	 		System.out.println("判断day2 - day1 : " + (day2-day1));
	 		return day2-day1;
	 	}
	 }

	/**
	 * 获取d1比d2多的月数
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int getMonthDiff(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
		int year1 = c1.get(Calendar.YEAR);
		int year2 = c2.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH);
		int month2 = c2.get(Calendar.MONTH);
		int day1 = c1.get(Calendar.DAY_OF_MONTH);
		int day2 = c2.get(Calendar.DAY_OF_MONTH);
		// 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
		int yearInterval = year1 - year2;
		// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
		if(month1 < month2 || month1 == month2 && day1 < day2) yearInterval --;
		// 获取月数差值
		int monthInterval =  (month1 + 12) - month2  ;
		if(day1 <= day2) monthInterval --;
		monthInterval %= 12;
		return yearInterval * 12 + monthInterval;
	}

	public static Date formatStringToDate(String date, SimpleDateFormat sdf) {
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			log.error("date:" + date + " format error");
			return null;
		}
	}

	public static String formatDateToString(Date date, SimpleDateFormat sdf) {
		return sdf.format(date);
	}

	/**
	 * 获取交专利的日期
	 * @return
	 */
	public static Date getNewDate(String str, Integer year){
		Date date = null;
		StringBuffer stringBuffer = new StringBuffer();
		Integer yearStr = Integer.parseInt(str.substring(0, 4));
		yearStr = yearStr + year - 1;
		if(StringUtils.isNotBlank(str)){
			stringBuffer.append(yearStr);
			stringBuffer.append(str.substring(4, str.length()));
			date = formatStringToDate(stringBuffer.toString(), new SimpleDateFormat("yyyy-MM-dd"));
		}
		return date;
	}

	/**
	 * 获取当前的除时分秒的日期
	 */
	public static Date getNowDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(new Date());
		Date date =  parseDateByPattern(s,"yyyy-MM-dd");
		return date;
	}

	/**
	 * 比较两个日期相差的天数（yyyy-MM-dd） second比first大的天数
	 * @param first
	 * @param second
	 * @return
	 * @throws Exception
	 */
	public static int  longOfTwoDate(Date first,Date second) throws Exception{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(first);
		int cnt = 0;
		while(calendar.getTime().compareTo(second)!=0){
			calendar.add(Calendar.DATE, 1);
			cnt++;
		}
		return cnt;
	}

	/**
	 * 算一个日期几个月后的日期
	 * @param olddate
	 * @param month
	 * @return
	 */
	public static Date getNewDate(Date olddate, Integer month) {
		Date  date  = olddate;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String data = format.format(date);
		String dataStr[] = data.split("-");
		//年份
		int year = (Integer.parseInt(dataStr[1]) + month)/12;
		//月份
		int yue = (Integer.parseInt(dataStr[1]) + month)%12;
		String a = "";
		if(yue<10){
			if(yue<1){
				a = "12";
			}else{
				a = "0"+ yue;
			}
		}else {
			a = yue + "";
		}
		dataStr[0]=String.valueOf(Integer.parseInt(dataStr[0]) + year);
		dataStr[1]=a;
		String  newdata  = dataStr[0]+"-"+dataStr[1]+"-"+dataStr[2];
		Date newDate = null;
		try {
			newDate = format.parse(newdata);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return newDate;
	}

}
